﻿/// <summary>
/// data   :2016-04-18
/// author :fanzhengyong
/// 闪烁字体组件
/// 1、直接拖到包含Text组件的父对象上或者从组件菜单上添加
/// 2、参数
///    interval   间隔时间，秒为单位可以小于1，但必须大于0
///    startColor 初始颜色
///    endColor   最终颜色
/// </summary>
using UnityEngine;
using System.Collections;
using UnityEngine.UI;



namespace UEngine
{
    [AddComponentMenu("Custom/UGUI/FlashFont")]
    public class FlashFont : MonoBehaviour 
    {
        public float  m_interval   = 1.0f;
        public Color  m_startColor = Color.red;
        public Color  m_endColor   = Color.black;

        private Text  m_textCom    = null;
        private bool  m_flag       = false;
        private float m_lerp       = 0.0f;

        void Start()
        {
            m_textCom       = gameObject.GetComponent<Text>();
            m_textCom.color = m_startColor;
        }

        void Update()
        {
            Flash();
        }

        void Flash()
        {
            if (m_textCom.color == m_startColor)
            {
                m_flag = false;
            }
            if (m_flag == false)
            {
                m_lerp          = Mathf.PingPong(Time.time, m_interval) / m_interval;
                m_textCom.color = Color.Lerp(m_startColor, m_endColor, m_lerp);
            }

            if (m_textCom.color == m_endColor)
            {
                m_flag = true;
            }
            if (m_flag == true)
            {
                m_lerp          = Mathf.PingPong(Time.time, m_interval) / m_interval;
                m_textCom.color = Color.Lerp(m_endColor, m_startColor, m_lerp);
            }
        }
    }
}

